package com.cetcs.kmga.dataView.controller;

import com.cetcs.kmga.common.BaseController;
import com.cetcs.kmga.common.BaseResponse;
import com.cetcs.kmga.common.Constant;
import com.cetcs.kmga.dataView.dao.DataSourceContextHolder;
import com.cetcs.kmga.dataView.entity.dataManager.vo.CircularChartVo;
import com.cetcs.kmga.dataView.entity.dataManager.vo.RankOfBehaviorVo;
import com.cetcs.kmga.dataView.entity.dataManager.vo.ResourceRiskEventVo;
import com.cetcs.kmga.dataView.service.IDataManagerService;

import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 类注释
 *
 * @author xutao
 * @version V1.0 创建时间：2018-01-23 15:57
 *          Copyright 2018 by CETC
 */
@RestController
@RequestMapping("/dataManager")
public class DataManagerController extends BaseController{

    @Autowired
    private IDataManagerService dataManagerService;

    /**
     * 统计某个系统的安全指数
     *
     * @return
     * @throws Exception
     */

    @RequestMapping(value = "/statSysSafeIndexById", method = RequestMethod.GET, produces = {"application/json", "application/xml"})
    public BaseResponse statSysSafeIndexById(@RequestParam(value = "id", required = false) String sysId) {
        try {
            //数据库切换
            DataSourceContextHolder.setDataSourceType("bddataSource");
            if ("cfc8bba9-bb30-11e7-89d7-0caf485aed97".equals(sysId)){
                return this.ok(dataManagerService.statSysSafeIndexById(sysId));
            }
            else{
                return this.ok();
            }
        } catch (Exception e) {
            e.printStackTrace();
            return this.error(Constant.SYSTEM_EXCEPTION);
        }
    }

    @RequestMapping(value = "/findNewlyRiskEvent", method = RequestMethod.GET)
    public BaseResponse findNewlyRiskEvent(@RequestParam(value = "id", required = false) String sysId) {
        try {
            //数据库切换
            DataSourceContextHolder.setDataSourceType("bddataSource");
            if ("cfc8bba9-bb30-11e7-89d7-0caf485aed97".equals(sysId)){
                List<ResourceRiskEventVo> result = dataManagerService.findNewlyRiskEvent(sysId);
                return this.ok(result);
            }
            else{
                return this.ok();
            }

        } catch (Exception e) {
            e.printStackTrace();
            return this.error(Constant.SYSTEM_EXCEPTION);
        }
    }

    @RequestMapping(value = "/findRankOfResourceAccess", method = RequestMethod.GET)
    public BaseResponse findRankOfResourceAccess(@RequestParam(value = "id", required = false) String sysId) {
        try {
            //数据库切换
            DataSourceContextHolder.setDataSourceType("bddataSource");
            if ("cfc8bba9-bb30-11e7-89d7-0caf485aed97".equals(sysId)){
                List<RankOfBehaviorVo> result = dataManagerService.findRankOfResourceAccess(sysId);
                return this.ok(result);
            }
            else{
                return this.ok();
            }
        } catch (Exception e) {
            e.printStackTrace();
            return this.error(Constant.SYSTEM_EXCEPTION);
        }
    }
    @RequestMapping(value = "/findRiskLevelStatistic", method = RequestMethod.GET)
    public BaseResponse findRiskLevelStatistic(@RequestParam(value = "id", required = false) String sysId) {
        try {
            //数据库切换
            DataSourceContextHolder.setDataSourceType("bddataSource");
            if ("cfc8bba9-bb30-11e7-89d7-0caf485aed97".equals(sysId)){
                CircularChartVo ret = dataManagerService.findRiskLevelStatistic(sysId);
                return this.ok(ret);
            }
            else{
                CircularChartVo vo = new CircularChartVo();
                vo.setDatas(Lists.newLinkedList());
                return this.ok(vo);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return this.error(Constant.SYSTEM_EXCEPTION);
        }
    }
}
